home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / text / print / deskjet500print.lha / a4print4.rexx next >
OS/2 REXX Batch file  |  1992-10-09  |  12KB  |  331 lines

  1. /*
  2. 4print.rexx version 2.2
  3.  
  4. + Script modified for papersize DIN A4 (ATN: Alley enlarged - upto 3 additional chars per line !)
  5. + automatic handling of twosided printjobs
  6. + partial translation of umlauts to the deskjet default charset PC8 
  7. + handling of backspaces (double strike typewriter mode :-))
  8.  
  9. Customization:
  10. -> for other paper formats, adjust localpagesize, psize,
  11.    change the settings for the various layout styles (width, oddleftborder)
  12.    and set the command sequence isoA4='' for default (should be US Letter)
  13.  
  14. Peter Jakobi, Munich, Oct. 2, 1992
  15. jakobi@informatik.tu-muenchen.de
  16. lazarus@salyko.doit.cubenet.org
  17.  
  18.  
  19. let me first quote from Georg Hessmann's MakeBatch.rexx (from MetaFont): */
  20. IF ~show('Libraries', 'rexxsupport.library') THEN DO
  21.         IF ~addlib('rexxsupport.library', 0, -30) THEN DO
  22.                 say "No RexxSupport ... no printing"
  23.                 EXIT 10
  24.         END
  25. END
  26. /*
  27. Original Posting:
  28.  
  29. Path: informatik.tu-muenchen.de!math.fu-berlin.de!Sirius.dfn.de!chx400!ira.uka.de!yale.edu!jvnc.net!darwin.sura.net!haven.umd.edu!uunet!olivea!tardis!jms
  30. From: jms@tardis.Tymnet.COM (Joe Smith)
  31. Newsgroups: alt.sources.amiga
  32. Subject: Program to print 4 pages on 1 piece of paper
  33. Summary: For HP DeskJet printer, written in AREXX
  34. Message-ID: <3111@tardis.Tymnet.COM>
  35. Date: 2 Sep 92 07:55:40 GMT
  36. Organization: BT Tymnet, San Jose, CA
  37. Lines: 140
  38. */
  39. /* Prints 4 pages of text on a single sheet of paper on a DeskJet 500 printer */
  40. /* Usage: print4 filename [width]                      */
  41. /*  where 'width' is optional, one of 89 (default), 60, 40 */
  42.  
  43. /* SOME REQUIREMENTS: SETUP MUST BE 'NO BORDERS', 'CHARSET PC8' */
  44.  
  45. parse upper arg option options 
  46.  
  47. if option='?' then do
  48.   say ''
  49.   say '4Print.rexx [<option>*] filename'
  50.   say 'where option is one of:'
  51.   say '   -2           Two sided printjob'
  52.   say '                (no manual sorting required)'
  53.   say '   -w<number>   line width of minipage'
  54.   say '   -D           quick draft mode instead of letter quality'
  55.   say ''
  56.   say 'On one sheet of paper4Print can print e.g.:'
  57.   say '   +  8 pages à 89 chars / 66 lines'
  58.   say '   + 16 pages à 41 chars / 66 lines'
  59.   say ''
  60.   say 'HP DESKJET ONLY!'
  61.   exit 1
  62. end
  63.  
  64. twopage=0                    /* Print on both sides ? off */
  65. basename = 't:4print~.'
  66. paper    = 0                    /* Outputfile and page */
  67.  
  68. /*          ' ä ü ö ß Ä Ü Ö £ ± § ­ ÷'  */
  69. inputtable ='E4FCF6DFC4DCD6A3B1A7ADF7'x        /* Ecma94latin1 - german subset */    
  70. outputtable='848194E18E9A999CF115C4F6'x        /* Hp pc8 - _THE_DEFAULT_ for all fonts */    
  71.  
  72. HT = d2c(09); LF = d2c(10); FF = d2c(12); CR = d2c(13); ESC = d2c(27)
  73.  
  74. /* localpagesize, *borders: changed for DIN A4: */
  75. /* US should be 60, 0, 0 */
  76.  
  77. localpagesize    = 66                /* Default pagesize of the minipages */
  78. evenleftborder   = 0
  79.  
  80. width             = 89                /* default */
  81.  
  82. draft=ESC'&k0W'ESC'*r2Q'            /* (default) uni direktional, LetterQuality */    
  83. cheap=ESC'&k1W'ESC'*r1Q'            /* bidirektional, 'draft' - might sometimes
  84.                                                    even be slower than uni ! */    
  85.  
  86. isoA4=ESC'&l26A'                /* A4 */
  87. psize=66*2+3+2+2
  88. psize=ESC'&l'psize'P'
  89.  
  90. do while left(option,1)='-'            /* Option parsing */
  91.   select
  92.     when option='-2' then twopage=1
  93.     when option='-D' then draft=cheap        /* quick and dirty printout */
  94.     when left(option,2)='-W'             /* Set width of minipage */
  95.       then do
  96.         width=substr(option,3)            /* Request twosided print */
  97.         if datatype(width)~='NUM' 
  98.           then do
  99.             say 'WARNING: Illegal width 'width
  100.             width=''
  101.             end
  102.         end
  103.     otherwise leave
  104.     end
  105.   parse var options option options
  106.   end
  107.  
  108. infile = option                    /* input file name */
  109.  
  110. if width = '' then width = 1000            /* For numeric comparisons below */
  111. if open('in',infile,'R') 
  112.   then if twopage then call open('out',basename||paper,'W')
  113.                   else call open('out','par:4.tmp','W')
  114.   else do; say "Can't open input file" infile; exit 10; end
  115. say 'infile='infile 'width='width
  116.  
  117. select
  118. /* 8'' PRINTABLE AREA + 1/8'' PER BORDER (HW!) -> 8.5'' DIN A4 WIDTH */ 
  119. /* 24 CPI -> 192 CHARS (182 and 10.2 border) */
  120. /* 20 CPI -> 160 CHARS (         8.4 border) */
  121. /* Separators = º (double pipe) ù (a small circle) */
  122.  
  123. /* For US Letter, you should remove .5 inch from the width, and
  124.    ca. 3/4 inch from the height (6 lines per minipage) */
  125.  
  126.   when width <= 44 then do    /* 24cpi 44+2+44+2+44+2+44 = 176 + 6 */
  127.     title = 'Letter Gothic, 4 columns of 44 chars, 6 char alley.'
  128.     font  = ESC'(s0p24h6v0s6T'
  129.     sep   = 'º '        
  130.     cross = 'ù-'
  131.     width = 44; columns = 4
  132. oddleftborder    = 10                /* Default oddside left margin */        
  133.     end
  134.   when width <= 60 then do    /* 24cpi 60+2+60+2+60 = 184 */
  135.     title = 'Letter Gothic, 3 columns of 60 chars, 3 char alley.'
  136.     font  = ESC'(s0p24h6v0s6T'
  137.     sep   = 'º '        
  138.     cross = 'ù-'
  139.     width = 60; columns = 3
  140. oddleftborder    = 8                /* Default oddside left margin */        
  141.     end
  142.   when width <= 79 then do    /* 20cpi 79+2+79 = 160 */
  143.     title = 'Courier, 2 columns of 79 chars, 2 char alley.'
  144.     font  = ESC'(s0p20h6v0s3T'
  145.     sep   = 'º '        
  146.     cross = 'ù-'
  147.     width = 79; columns = 2
  148. oddleftborder    = 0                /* Default oddside left margin */        
  149.     end
  150.   when width = 80 then do    /* 20cpi 80+0+80 = 160 */
  151.     title = 'Courier, 2 columns of 80 chars, 0 char space! ----> VIEL ZU ENG !!!'
  152.     font  = ESC'(s0p20h6v0s3T'
  153.     sep   = ''
  154.     cross = ''
  155.     width = 80; columns = 2
  156. oddleftborder    = 0                /* Default oddside left margin - out of space ... */        
  157.     end
  158.   otherwise do            /* 24cpi 89+4+89 = 182 */
  159.     title = 'Letter Gothic, 2 columns of 89 chars, 3 char alley.'
  160.     font  = ESC'(s0p24h6v0s6T'
  161.     sep   = ' º  '        
  162.     cross = '-ù--'
  163.     width = 89; columns = 2
  164. oddleftborder    = 10                /* Default oddside left margin */        
  165.     end
  166.   end
  167. say title                    /* Let user know what has been selected */
  168.  
  169. sep1 = ''; sep2 = ''                /* Build the horizontal dividing line */
  170. do column = 1 to columns-1
  171.   sep1 = sep1 || copies(' ',width) || sep
  172.   sep2 = sep2 || copies('-',width) || cross    /* Dashes and plus signs */
  173.   end
  174. sep2 = sep2 || copies('-',width)        /* 'XYZABCDEF' */
  175.  
  176. call writech('out',isoA4||ESC'&k2G'ESC'&s1C'ESC'&k6W'draft)    /* LF=CR+LF, no linewrap, textscale mode, draft ? */
  177. call writech('out',ESC'&l12d126p0L'font||ESC'&l0F'psize)    /* 12 LPI, no perforation skip, bottom 1 inch */
  178. if twopage then call close('out')
  179.  
  180. /* Process the single input file */
  181.  
  182. leftborder = copies(' ',oddleftborder)
  183. prevline = ''
  184. do until eof('in')
  185.   paper = paper + 1                /* the sheet of paper to print */
  186.   if twopage then do
  187.     if paper//2 then leftborder = copies(' ',oddleftborder)
  188.     else leftborder = copies(' ',evenleftborder)
  189.     call open('out',basename||paper,'W')
  190.     end
  191.   LINES. = copies(' ',width)        /* Fill array with blanks */
  192.   title = infile', page' paper', printed' date('weekday')',' ,
  193.     date('normal') time('normal')
  194.   say title                /* Show progress on the console */
  195.   do page = 0 to (columns*2)-1        /* Top row, bottom row, N columns */
  196.     do pageline = 1 to localpagesize
  197.       line = (page*localpagesize)+pageline
  198.       if length(prevline) > 0
  199.         then temp = prevline        /* If previous char was a formfeed */
  200.     else temp = translate(readln('in'),outputtable,inputtable)    /* Else read line from input file */
  201.       prevline = ''
  202.       i = index(temp,FF)        /* Look for formfeeds */
  203.       select
  204.     when i = 0 then nop        /* Nothing special if no formfeed */
  205.     when i = 1 & pageline = 1 then ,
  206.       temp = substr(temp,2)        /* Ignore FF if at top of page */
  207.     when i = 1 & length(temp) = 1 then ,
  208.       leave pageline        /* FF+LF = end of current page */
  209.     when i = 1 then do        /* FF+TEXT+LF */
  210.       prevline = substr(temp,2)    /* The TEXT goes on next page */
  211.       leave pageline        /* Finished with current page */
  212.       end
  213.     otherwise do            /* TEXT+FF+LF or TEXT+FF+TEXT+LF */
  214.       prevline = substr(temp,i)    /* Remember FF and possible text */
  215.       temp = left(temp,i-1)        /* Process text that's before the FF */
  216.       end
  217.     end
  218.       i = index(temp,HT)        /* Look for tabs */
  219.       do while i > 0
  220.         blanks = 8 - ( (i-1) // 8)    /* Replace tab with this many blanks */
  221.         if i = 1 then              temp = copies(' ',blanks)||substr(temp,i+1)
  222.       else temp = substr(temp,1,i-1)||copies(' ',blanks)||substr(temp,i+1)
  223.         i = index(temp,HT)        /* Check multiple tabs on a line */
  224.         end
  225.       temp=strip(temp,'T')        /* Remove trailing blanks */        
  226.  
  227.       bswidth=0                /* add 2 chars for each backspace */
  228.       bsindex=0
  229.       do forever
  230.         bsindex=index(temp,'08'x,bsindex+1)
  231.         if bsindex=0 then leave        /* Done */
  232.         if bsindex>width+bswidth then leave    /* BS not in current line */
  233.         bswidth=bswidth+2
  234.       end
  235.  
  236.       LINES.line = left(temp,width+bswidth,' ')    /* Truncate, or pad with blanks */
  237.       if substr(temp,width+1+bswidth)=='' then prevline=''
  238.       else prevline='-->> 'substr(temp,width+1+bswidth)    /* The rest of the processed line or empty */
  239.       end
  240.     end
  241.  
  242.   /* Now that a paperful of data has been read, print it out. */
  243.  
  244.   call writeln('out',leftborder||center(title,width*columns+(columns-1)*length(sep)))
  245.   call writeln('out','')
  246.   do row = 0 to 1            /* Top half of page, then bottom half */
  247.     do pageline = 1 to localpagesize
  248.       temp = ''
  249.       do column = 1 to columns
  250.         line = (row*localpagesize*columns) + (column-1)*localpagesize + pageline
  251.         temp = temp||LINES.line
  252.     if column < columns then temp = temp||sep
  253.     end
  254.       if pageline = localpagesize & row = 1
  255.       then call writech('out',leftborder||temp||CR||FF)    /* Formfeed at end of page */
  256.       else call writech('out',leftborder||temp||LF)    /* Linefeed for all others */
  257.       end    /* next pageline */
  258.     if row = 0 then call writech('out',leftborder||sep1||LF||leftborder||sep2||LF||leftborder||sep1||LF) /* Dashes */
  259.     end        /* next row (now do bottom half of page) */
  260.   if twopage then call close('out')
  261.   end        /* until EOF */
  262.  
  263. call close('in')
  264.  
  265. if twopage then do
  266.                         /* Now copy the pages to the printer */
  267.  
  268.                         /* print p2 p4 <ff> <reinsert> p5 p3 p1 */
  269.   numerror=0
  270.   evenskip=paper//2
  271.   call open('out','par:4.prt','W')
  272.   call copyfile(0)                /* Copy the printer prologue */
  273.  
  274.   if paper~=1 then do                /* No need to print the next line :-) */
  275.     do i = 2 to paper-evenskip by 2        /* Print the even pages */
  276.        call copyfile(i)
  277.        end    
  278.     if evenskip then call writech('out',FF)    /* Skip the empty last evenside page ? */
  279.  
  280.     say 'Please reinsert the output - page 2 on bottom blank side down'
  281.     pull temp
  282.  
  283.     end
  284.  
  285.   if evenskip then paper1=paper            /* BUGFIX, again, oct 7 1992 */
  286.   else paper1=paper-1
  287.   do i = paper1 to 1 by -2            /* Print odd pages reversed */
  288.     call copyfile(i)
  289.     end    
  290.  
  291.   if numerror=0 then do page=0 to paper        /* If there were no errors - erase temp files */
  292.     call delete(basename||page)
  293.     end
  294.   else say 'There were errors: the pages are 'basename'<count> [0 is the printer init]'
  295.   end
  296.  
  297. call close('out')                /* One-sided: we already printed, so skip the above */ 
  298.  
  299. 'initprinter'                    /* Reinitialize the printer */
  300. exit 0
  301.  
  302.  
  303. /********************************************/  /* Subroutines */
  304.  
  305. copyfile: procedure expose basename numerror    /* Print a page */
  306.   arg page
  307.   if ~open('in',basename||page,'R') 
  308.     then do 
  309.       call writech('out',FF)            /* Skip the page */
  310.       say 'ERROR: Could not print page 'i' - skipping'
  311.       numerror = 1    
  312.       end
  313.     else do
  314.       do forever                /* Copy the page */
  315.         temp=readch('in',20000)
  316.         call writech('out',temp)
  317.         if temp=='' then leave
  318.         end
  319.       call close('in')
  320.       end
  321. return
  322.  
  323. /* End of s:print4 */
  324. /*
  325. -- 
  326. Joe Smith (408)922-6220     BTNA GNS Major Programs, TYMNET Global Network
  327. <jms@tardis.tymnet.com>     P.O. Box 49019, MS-C51, San Jose, CA 95161-9019
  328. CA license plate: "POPJ P,"    Married to the LB, Quantum Leap's #1 net.fan
  329. PDP-10, 36-bits forever!    Humorous disclaimer: "My Amiga 3000 speaks for me."
  330. */
  331.